#include <QtCore>
#include <assert.h>

void usage( int argc, char * argv[] ) 
{
	fprintf(stderr,"usage:\n"
		"\t %s inputDiskEntryFile inputLogFile \n",argv[0]);
	exit(1);
}

struct  timeData
{
	timeData(){inTime = outTime = line = ""; }
	timeData(QString in_time, QString out_time, QString _line)
		:inTime(in_time), outTime(out_time), line(_line)
	{};
	QString inTime, outTime, line;
	//int loginID;
	inline int timeBetween(QString time){
		if(time<inTime)	//less than
			return -1;
		if(time>outTime) //greater than
			return 1;
		return 0; //equal
	}
	// 	timeData _tmp("a", "b", "c", 1);
};

int main(int argc, char* argv[])
{
	QCoreApplication a(argc, argv);
//  timeData _tmp("2011/07/25_12:42:44.130", "2011/07/25_13:17:10.979", "c", 1);
// 	bool __b1 = _tmp.timeBetween("2011/07/25_13:17:11.001");
// 	bool __b2 = _tmp.timeBetween("2011/07/25_13:17:10.980");
// 	bool _b3 = QString("abc")<QString("abcd");
// 	bool _b4 = QString("abc")<QString("abd");
// 	QString __tmp = "0x13";
// 	bool ok;
// 	int ___tmp = __tmp.toInt(&ok, 16);
	QTextStream qout(stdout);
#ifdef _DEBUG
	argc = 3;
	argv[1] = "./../Debug/d.csv";
	argv[2] = "./../Debug/eventLog.csv";
#endif // _DEBUG
	if(argc < 3)
		usage(argc, argv);
	QFile file_disk(argv[1]), file_log(argv[2]);
	if (!file_disk.open(QIODevice::ReadOnly | QIODevice::Text) ||  !file_log.open(QIODevice::ReadOnly | QIODevice::Text)){
		printf("file read error.\n");	
		exit(1);
	}
	QTextStream in_disk(&file_disk), in_log(&file_log);
	QString title_disk = in_disk.readLine(), title_log = in_log.readLine();
 	if(title_disk.isEmpty() || title_log.isEmpty())	{
 		printf("File empty.\n");
 		exit(1);
 	}
// 	if(title_disk[title_disk.size()-1] != ',')
// 		title_disk += ',';
	qout<<title_disk.section(',', 0, 5)<<","<<title_log<<","<<title_disk.section(',', 6)<<endl;
	QList<timeData>timePairs; //login, logout
	while(!in_log.atEnd()){
		QString curLine = in_log.readLine();
		QString inTime = curLine.section(',', 0, 0);
		QString outTime = curLine.section(',', 1, 1);
		if(!inTime.isEmpty() && !outTime.isEmpty()){
// 			QString userName = curLine.section(',', 2, 2);
// 			bool ok = false;
// 			int loginID = curLine.section(',', 3, 3).toInt(&ok, 16);
			timePairs.push_back( timeData(inTime, outTime, curLine) );
		}
	}
//traverse in_disk
	while(!in_disk.atEnd()){
		QString curLine = in_disk.readLine(); //looks like 0,".\$MFT",3088464821,0,2010/09/24_09:45:42.963,FNM.FNA.FNC.FNE.,other,,,
		qout<<curLine.section('\"', 0, 1)<<"\"";//looks like 0,".\$MFT
		curLine = curLine.section('\"',2); //looks like ,3088464821,0,2010/09/24_09:45:42.963,FNM.FNA.FNC.FNE.,other,,,
		qout<<curLine.section(',', 0, 4)<<","; //,3088464821,0,2010/09/24_09:45:42.963,FNM.FNA.FNC.FNE.
		QString lastModifyTime = curLine.section(',',3,3);
		for(int i=0; i<timePairs.size(); i++){
			timeData &td = timePairs[i];
			int re = td.timeBetween(lastModifyTime);
			if( re == 0){
				qout<<td.line;
				goto afterOutputTime;
			}
		}
		qout<<",,,";
afterOutputTime:
		qout<<","<<curLine.section(',', 5)<<endl;
	}
	return 0;
}

